package com.example.j280_webproject.userConsole.servlet;

import com.example.j280_webproject.userConsole.service.PageService;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/page")
public class PageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String showCount = req.getParameter("showCount");
        String page = req.getParameter("page");

        if(showCount == null || "".equals(showCount))
            showCount = "5";                              // 默认值为每页显示5条
        if(page == null || "".equals(page))
            page = "1";                                   // 默认值为页码第一页

        JSONObject jsonObject = new PageService().page(showCount, page);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(jsonObject);
    }
}
